﻿using Download;
using StockDataBase.models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 股票数据;

namespace StockDataBase.Download
{
    public class DownReports
    {
        private DownReports()
        {

        }

        private DownBase down = new DownBase();
        private static DownReports ins = new DownReports();

        public static DownReports Instance
        {
            get { return ins; }
        }

        private void run(Action ac)
        {
            MainWindow.Instance.Dispatcher.BeginInvoke(ac);
        }


        private void down_sh()
        {
            var res = Utils.DB_Client.Select<StockInfo>("select * from StockInfo where stockId like 'sh%'");
            var res2 = Utils.DB_Client.Select<StockInfo>("select * from StockInfo where stockId like 'sz%'");

            foreach(var item in res2)
            {
                res.Add(item);
            }

            run(()=>{
                MainWindow.Instance.pb.Maximum = res.Count;
                MainWindow.Instance.pb.Value = 0;
                
            });

            int idx = 0;

            foreach(var item in res)
            {
                var report = down.DownString(@"http://f10.eastmoney.com/f10_v2/FinanceAnalysis.aspx?code="  + item.stockId,Encoding.UTF8 );
                var sw = new StreamWriter(Utils.REPORT_PATH + item.stockId + ".html");
                sw.Write(report);
                sw.Close();
                idx++;
                LogUtils.Print(item.name +  "报表下载成功");
                run(() =>
                {
                    MainWindow.Instance.pb.Value = idx;
                });
            }
        }


        public void DownAllReports()
        {
            down_sh();
        }




    }




}
